package domain;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import java_persistence.PatientMapper;
import java_persistence.TreatmentRecordMapper;

import org.apache.ibatis.session.SqlSession;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class Reports {

	private List<Float> bills;
	private JFreeChart weightChart;
	private ChartFrame frame;
	private SqlSession ses;
	
	/**
	 * Creates the reports instance with the current sequel session
	 * 
	 * @param session the current sequel session
	 */
	public Reports(SqlSession session){
		ses = session;
	}
	
	/**
	 * sets bills for a given month
	 * 
	 * @param month The month to check
	 */
	public void setMonth(String month){
		bills = get_prices_for_month(month, ses);
	}
	
	
	/**Calculates the total amount billed in a given
	 * month as a String.
	 *
	 *@return ret the total amount billed as a string
	*/
	public String totalBilledForMonth(){
		Float retVal = new Float(0);
		String ret;
		Iterator<Float> itr = bills.iterator();
		
		while (itr.hasNext())
			retVal = retVal + itr.next();
		
		ret = retVal.toString();
		
		return ret;
	}
	
	/**String representing the total number of patients seen from the given month
	 * 
	 * @return patients seen as a String
	 */
	public String totalPatientsSeen(){
		Integer der = new Integer(bills.size());
		return der.toString();
	}
	
	/**Generates the chart object for a given patient's information
	 * 
	 * @param patient The patient's name
	 */
	public void patientChart(String patient){
		PatientMapper map = ses.getMapper(PatientMapper.class);
		List<Integer> weights = get_weights_for_patient(map.get_patient_by_username(patient), ses);
		Iterator<Integer> itr = weights.iterator();
		DefaultCategoryDataset set = new DefaultCategoryDataset();
		int i = 0;
		
		while (itr.hasNext())
			set.addValue(itr.next(),String.valueOf(++i),patient);
		
		weightChart = ChartFactory.createLineChart(patient, "Visit", "Weight (in lbs)", set, PlotOrientation.VERTICAL, false, false, false);
		frame = new ChartFrame("Weight", weightChart);
	}
	
	/**Gets the panel for the chart
	 * 
	 * @return Returns the Chart Panel
	 */
	public ChartPanel getPanel(){
		return frame.getChartPanel();
	}

	public static List<Integer> get_weights_for_patient(Patient p, SqlSession session) {
		TreatmentRecordMapper trmap = session.getMapper(TreatmentRecordMapper.class);
		List<Integer> ret_val = new ArrayList<Integer>();
		
		ret_val=trmap.get_patient_weights(p.getUsername());

		return ret_val;
	}
		
	/**
	 * @param month - a 2-character string such as "01" for January or "12" for December.
	 * @param session - the sequel session for the application
	 * @return a list of Floats representing the total for each appointment billed. 
	 */
	public static List<Float> get_prices_for_month(String month, SqlSession session) {
		List<Float> ret_val = new ArrayList<Float>();
		TreatmentRecordMapper trmap = session.getMapper(TreatmentRecordMapper.class);
			
		ret_val = trmap.get_prices_for_month(month);

		return ret_val;
	}

}


